$(function() {
	$("#addt").click( function () { $("#addteacher").dialog("open"); });
	//开班
	$("#addclasses").click( function () { 
		//取课程
		var cou = $("#course");
		if( cou.children().length==0 ){
			$.get("/course/category/getcategorynopar.do",{type:1,categoryId:null, _:(new Date()).getTime() },function(data){
				if(data!=""){
					
					cou.append("<option value=''>=请选择=</option>");
					for ( var i = 0; i < data.length; i++) {
						cou.append("<option value='"+data[i].id+"'>"+data[i].name+"</option>");
					}
				}
			},"json");
		}
		
		//取地点
		var a = $("#place");
		if( a.children().length==0 ){
			$.get("/course/category/getcategories.do",{type:6},function(data){
				if(data!=""){
					
					a.append("<option value=''>=请选择=</option>");
					for ( var i = 0; i < data.length; i++) {
						a.append("<option value='"+data[i].id+"'>"+data[i].name+"</option>");
					}
				}
			},"json");
		}
		
		$("#kb").dialog("open");
	});
	
	//预定班级名称
	$("#course,#place").change(function(){
		if( this.value!="" && $(":radio[name='isVIP'][value='0']").attr("checked")=="checked" ){
			var pname="",cname="";
			if( $("#place").val()!="" ){
				pname = $("#place").find("option:selected").text();
			}
			if( $("#course").val()!="" ){
				cname = $("#course").find("option:selected").text();
			} 
			$("#className").val(pname+"_"+cname);
		}
	});
	
	//打开添加授课地点
	$("#addplace").click( function () { $("#tjdd").dialog("open"); });
	//打开课程列表
	$("#showclist").click( function () { $("#kclist").dialog("open"); });
	//根据地点选班级
	$("#placeid").change(function(){
		if( this.value!="" ){
			/*$.get("/course/classes/getbyplace.do",{placeId:this.value},function(data){
				if(data!=""){
					var chours,a = $("#classesId");
					a.empty();
					a.append("<option value=''>=请选择=</option>");
					for ( var i = 0; i < data.length; i++) {
						if( data[i].course.classHour == null ){
							chours = "";
						}else{
							chours = "("+data[i].course.classHour+")";
						}
						a.append("<option value='"+data[i].id+"'>"+data[i].course.name+chours+"</option>");
					}
				}
			},"json");*/
			
			$("select[name='shortcutplace']").val(this.value);
		}
	});
	//根据班级选课程
	$("#classesId").change(function(){
		if( this.value!="" ){
			classcousersetting.async.otherParam.classId= this.value;
			$.fn.zTree.init($("#classcoursetree"), classcousersetting);
			
			//根据班级给出默认授课地点
			if( this.value!="" ){
				$("#placeid").val( classAndPlace.get( this.value ) );
				$("select[name='shortcutplace']").val( classAndPlace.get( this.value ) );
			}
		}
	});
	//点击排课窗口的选课程input
	$("#classcourseid").click( function(){
		var cityObj = $("#classcourseid");
		var cityOffset = $("#classcourseid").offset();
		var arrangeOffset = $("#arrange").offset();
		$("#menuContent").css({left:cityOffset.left-arrangeOffset.left + "px", top:cityOffset.top-arrangeOffset.top+cityObj.outerHeight() + "px"}).slideDown("fast");

		$("body").bind("mousedown", onBodyDown);
	} );
	//打开教师列表窗口
	$("#showtlist").click(function(){
		getTeachers("teacherList");
		$("#teacherlist").dialog("open");
	});
	//打开授课地点列表窗口
	$("#showplist").click(function(){
		getTeachPlace("placeList");
		$("#placelist").dialog("open");
	});
	//打开开班列表窗口
	$("#showclaslist").click(function(){
		$.get("/course/classes/getclaslist.do",function(data){
			if(data!=""){
				var a = $("#claslt");
				a.empty();
				a.append("<tr><th>已开班级</th><th>授课地点</th><th>课程</th><th>备注</th><th>操作</th></tr>");
				for ( var i = 0; i < data.length; i++) {
					var remar = data[i].remark!=null?data[i].remark:"";
					a.append("<tr><td>"+data[i].className+"</td><td>"+data[i].place.name+"</td><td>"+data[i].course.name+"</td><td>"+remar+"</td><td></td></tr>");
				}
			}
		},"json");
		$("#classlist").dialog("open");
	});
	//复制和取消复制
	$("#copyarr,#cancelcopy").click(function(){
		if($(this).attr("id")=="copyarr"){
			iscopyarr = true;
			$(this).hide();
			$("#cancelcopy").show();
			$("#arrange").dialog("close");
		}else{
			iscopyarr = false;
			$(this).hide();
			$("#copyarr").show();
		}
	});
	//删除排课
	$("#delarr").click(function(){
		if( confirm("确定要删除吗？") ){
			$.get("/course/arrange/delete.do",{id:$("#arrangeid").val()},function(data){
				if(data!=null){
					//$("#savea").clearForm();
					$('#calendar').fullCalendar('removeEvents',[data]);
					$("#arrange").dialog("close");
				}
			},"json");
		}
	});
	//open search
	$("#search").click(function(){
		getTeachers("searchTeacher");
		getTeachPlace("searchPlace");
		$("#searchdialog").dialog("open");
	});
	//添加课程按钮绑定事件
	$("#tjkctree").live("click", function(){
		$("#tjkc").dialog("open");
	});
	//打开帐号管理窗口
	$("#accountmc").click(function(){
		$.get("/course/user/getuserbyid.do",{id:$("#userid").val()},function(data){
			if(data!=""){
				$("#username").val( data.name);
				$("#userln").val( data.loginName);
				$("#email").val( data.mail );
				$("#roles").val( data.roles );
			}
		},"json");
		$("#accountm").dialog("open");
	});
	//修改密码
	$("#uppass").click(function(){
		var str = '<tr><th>新密码：</th><td><input type="password" name="password" id="userpass"/></td></tr>'+
				  '<tr><th>请再次确认密码：</th><td><input type="password" name="checkpass" id="checkpass"/></td></tr>';
		if( $("#saveaccount").html().indexOf("新密码")>0 ){
			$("#upptr").nextUntil("#sutr").remove();
		}
		else{
			$(str).insertAfter("#upptr");
		}
	});
	//设置快捷时间
	$("#shortcut").click(function(){
		var s = $("#startTime");
		var e = $("#endTime");
		var st = seldate + " " + s.val();
		var et = seldate + " " + e.val();
		var stime = s.val().substring(0,s.val().length-3);
		var etime = e.val().substring(0,e.val().length-3);
		if( s.val()!="" && e.val()!="" ){
			$.post("/course/category/save.do",{ startTime : st , endTime : et , type : 2 , name:"快捷时间" },function(data){
				if(data){
					alert("添加成功！");
					var str = "<span class=\"shortcut_span\" name=\"selscut\"><a href=\"#\" onclick=\"selShortcut('"+st+"','"+et+"')\">"+stime+"-"+etime+"</a><a href=\"#\" class='del_icon' onclick=\"delscut('"+data+"',this)\"></a></span>";
					$("#scutdiv").append(str);
					$("select[name='shortcutplace']").each(function(){
						var sctv = $(this).prev().prev().val();
						var sctsz = sctv.split("-");
						var day = sctsz[0];
						if(day!=""){
							var strlaw = '<input type="checkbox" name="arrangeLaw" value="'+day+'-'+data+'"/> '+stime+'<br />';
							$(strlaw).insertBefore(this);
						}
					});
					
				} 
			},"json");
		}
		else{
			alert("请选择开始和结束时间！");
		}
	});
	//变换快捷时间样式
	$("span[name='selscut']").live("mousemove",function(){
		$(this).children(".del_icon").show();
	}).live("mouseout",function(){
		$(this).children(".del_icon").hide();
	});
	//点选规律排课时间
	$(":checkbox[name='arrangeLaw']").live("click",function(){
		if( this.checked==true ){
			var placeid = $(this).nextAll("select").val();
			$(this).val( this.value + "-" + placeid );
		}
	});
	//选择规律排课地点
	$("select[name='shortcutplace']").change(function(){
		var placeid = this.value;
		var cs = $(this).prevAll(":checkbox[name='arrangeLaw']");
		for ( var i = 0; i < cs.length; i++) {
			if($(cs[i]).attr("checked") == "checked"){
				var a = cs[i].value.split("-");
				if( a.length==3 ){
					$(cs[i]).val( (cs[i].value).substring( 0,cs[i].value.length-1 ) + placeid );
				}
				else{
					$(cs[i]).val( cs[i].value + "-" + placeid );
				}
			}
		}
	});
	//是否VIP
	$(":radio[name='isVIP']").click(function(){
		if( $(this).val() == "1" ){
			$("#bjmc").html("VIP");
		}
		else{$("#bjmc").html("班级");}
	});
});

